/**
 * Copyright (c) 2011-2016, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.danskin.wechat.util;

import java.io.File;
import java.util.Random;
import java.util.UUID;

/**
 * StrKit.
 */
public class StringUtil {

	/**
	 * 首字母变小写
	 */
	public static String firstCharToLowerCase(String str) {
		char firstChar = str.charAt(0);
		if (firstChar >= 'A' && firstChar <= 'Z') {
			char[] arr = str.toCharArray();
			arr[0] += ('a' - 'A');
			return new String(arr);
		}
		return str;
	}

	/**
	 * 首字母变大写
	 */
	public static String firstCharToUpperCase(String str) {
		char firstChar = str.charAt(0);
		if (firstChar >= 'a' && firstChar <= 'z') {
			char[] arr = str.toCharArray();
			arr[0] -= ('a' - 'A');
			return new String(arr);
		}
		return str;
	}

	/**
	 * 字符串为 null 或为 "" 时返 true
	 */
	public static boolean isBlank(String str) {
		return str == null || "".equals(str.trim());
	}

	public static boolean isNullOrEmpty(String str) {
		return str == null || str.isEmpty();
	}

	/**
	 * 字符串不 null 而且不为 "" 时返true
	 */
	public static boolean notBlank(String str) {
		return str != null && !"".equals(str.trim());
	}

	public static boolean notBlank(String... strings) {
		if (strings == null)
			return false;
		for (String str : strings)
			if (str == null || "".equals(str.trim()))
				return false;
		return true;
	}

	public static boolean notNull(Object... paras) {
		if (paras == null)
			return false;
		for (Object obj : paras)
			if (obj == null)
				return false;
		return true;
	}

	public static String toCamelCase(String stringWithUnderline) {
		if (stringWithUnderline.indexOf('_') == -1)
			return stringWithUnderline;

		stringWithUnderline = stringWithUnderline.toLowerCase();
		char[] fromArray = stringWithUnderline.toCharArray();
		char[] toArray = new char[fromArray.length];
		int j = 0;
		for (int i = 0; i < fromArray.length; i++) {
			if (fromArray[i] == '_') {
				// 当前字符为下划线时，将指针后移一位，将紧随下划线后面几个字符转成大写并存放
				i++;
				if (i < fromArray.length)
					toArray[j++] = Character.toUpperCase(fromArray[i]);
			} else {
				toArray[j++] = fromArray[i];
			}
		}
		return new String(toArray, 0, j);
	}

	public static String join(String[] stringArray) {
		StringBuilder sb = new StringBuilder();
		for (String s : stringArray)
			sb.append(s);
		return sb.toString();
	}

	public static String join(String[] stringArray, String separator) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < stringArray.length; i++) {
			if (i > 0)
				sb.append(separator);
			sb.append(stringArray[i]);
		}
		return sb.toString();
	}

	public static String getUUID() {
		return UUID.randomUUID().toString();
	}

	public static String getShortUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	/**
	 * 删除起始字符
	 * 
	 * @param s
	 * @return
	 * @author xxj 2017年4月27日
	 */
	public static String trimStart(String str, String trim) {
		if (str == null)
			return null;
		return str.replaceAll("^(" + trim + ")+", "");
	}

	/**
	 * 删除末尾字符
	 * 
	 * @param s
	 * @return
	 * @author xxj 2017年4月27日
	 */
	public static String trimEnd(String str, String trim) {
		if (str == null)
			return null;
		return str.replaceAll("(" + trim + ")+$", "");
	}

	public static String combine(String... args) {
		if (args == null || args.length == 0)
			return "";
		StringBuffer sbf = new StringBuffer();
		for (String s : args) {
			// //纯协议开头不处理，如：http://,d:/,linux首个/不处理
			// if(s.matches("^[a-zA-z]+://$")){
			// sbf.append(s);
			// continue;
			// }
			// 首位地址只删除尾部正反斜杠
			if (sbf.length() == 0) {
				sbf.append(s.replaceAll("/{1,}$|\\{1,}$", ""));
				continue;
			}

			if (sbf.length() > 0)
				sbf.append("/");
			// 去除首尾正反斜杠
			sbf.append(s.replaceAll("^/{1,}|^\\{1,}", "").replaceAll("/{1,}$|\\{1,}$", ""));
		}

		return sbf.toString();
	}

	/**
	 * 将emoji表情替换成*
	 * 
	 * @param source
	 * @return 过滤后的字符串
	 */
	public static String filterEmoji(String source) {
		if (!isNullOrEmpty(source)) {
			return source.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "*");
		} else {
			return source;
		}
	}

	/**
	 * 产生随机的六位数
	 * 
	 * @return
	 */
	public static final char[] chars = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
	public static Random random = new Random();

	public static String getRandomNum() {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i <= 5; i++) {
			sb.append(chars[random.nextInt(chars.length)]);
		}
		return sb.toString();
	}
}
